(function($) {
	/***************************************************************************
	 * JQuery 表单输入插件12.0
	 * 
	 * Author: 姜志光 Date: 2014年1月8日
	 **************************************************************************/

	FormProcess = function(rootElement, userSettings) {

		var instance = this;

		/***********************************************************************
		 * 表单输入集合
		 **********************************************************************/
		var formInputs = new Array();

		this.formOperate;

		/***********************************************************************
		 * 视图初始化
		 **********************************************************************/
		this.init = function() {

			var operateConfig = userSettings.operateConfig;
			if (undefined != operateConfig) {
				instance.formOperate = new ViewOperate(operateConfig,
						$("#operateDiv"));
				instance.formOperate.operateDraw();
			}
			// 窗口高度调整
			// var height = 30;
			// var flowSubmitDiv = $(".flowSubmitDiv");
			// if (flowSubmitDiv.length == 1) {
			// height += flowSubmitDiv.height();
			// }
			// height += $(".toolDiv").height();
			// $('body').layout('panel', 'north').panel('resize', {
			// height : height
			// });
			// $('body').layout('resize');
		};

		/***********************************************************************
		 * 流程-审批同意处理
		 **********************************************************************/
		this.onAgreeForm = function() {
			$("#bagree").val(1);
			$('#checkform').ajaxSubmit(function(data) {
				if ("成功保存表单数据" == data) {
					alert("成功保存表单数据");
					if ("undefined" != typeof (window.parent.onRefresh)) {
						window.parent.onRefresh();
					}
					// returnView();
					refreshForm();
				} else if (data.indexOf("<input") == 0) {
					alert("成功保存表单数据");
					$("#refreshFormDiv").append(data);
					if ("undefined" != typeof (window.parent.onRefresh)) {
						window.parent.onRefresh();
					}
					instance.rerefreshForm();
				} else {
					alert(data);
				}
			});
		};

		/***********************************************************************
		 * 流程-审批拒绝处理
		 **********************************************************************/
		this.onDenyForm = function() {
			$("#bagree").val(-1);
			$('#checkform').ajaxSubmit(function(data) {
				if ("成功保存表单数据" == data) {
					alert("成功保存表单数据");
					if ("undefined" != typeof (window.parent.onRefresh)) {
						window.parent.onRefresh();
					}
					// returnView();
					refreshForm();
				} else if (data.indexOf("<input") == 0) {
					alert("成功保存表单数据");
					$("#refreshFormDiv").append(data);
					if ("undefined" != typeof (window.parent.onRefresh)) {
						window.parent.onRefresh();
					}
					refreshForm();
				} else {
					alert(data);
				}
			});
		};

		/***********************************************************************
		 * 流程历史记录查看
		 **********************************************************************/
		this.showHistory = function(title) {

			var win = window
					.open(
							"about:blank",
							"historyWindow",
							"height=450,width=800,scrollbars=yes,status=yes,toolbar=no,menubar=no,location=no");
			win.focus();
			$("#historyForm").submit();
		};

		/***********************************************************************
		 * 表单刷新
		 **********************************************************************/
		this.refreshForm = function() {
			$("#refreshForm").submit();
		};

		this.returnView = function(fromView) {
			if ("" != fromView) {
				window.location.href = application + '/goView?vv_guid='
						+ fromView;
			}
		};

		this.saveForm = function() {
			var beforeCheck = onBeforeSave();
			if (undefined != beforeCheck && typeof (beforeCheck) == "boolean"
					&& false == beforeCheck) {
				return;
			}
			// 主表单数据填充
			$("#infosDiv").html("");

			// 主表单数据保存对象
			var mainDatas = new Object();
			for ( var x = 0; x < mainFormInputs.length; x++) {
				var formInput = mainFormInputs[x];
				var tt_name = formInput.showTT_name();
				if (undefined == tt_name || "" == tt_name) {
					continue;
				}
				// 主表单输入数据
				var mainInputDatas = formInput.inputChecks();
				if ("boolean" == typeof (mainInputDatas)
						&& false == mainInputDatas) {
					return;
				}
				if (undefined != mainDatas[tt_name]) {
					for ( var key in mainInputDatas) {
						mainDatas[tt_name][key] = mainInputDatas[key];
					}
				} else {
					mainDatas[tt_name] = mainInputDatas;
				}

			}

			for ( var key in mainDatas) {
				var input = $("<input type='hidden' name='mainDatas." + key
						+ "'/>");
				input.val($.toJSON(mainDatas[key]));
				$("#infosDiv").append(input);
			}

			// 子表单数据填充

			$("#childDatasDiv").html("");
			for ( var childID in childFormInputs) {
				var formInputs = childFormInputs[childID];

				var childDatas = new Object();
				for ( var x = 0; x < formInputs.length; x++) {
					var formInput = formInputs[x];
					var tt_name = formInput.showTT_name();

					if (undefined == tt_name || "" == tt_name) {
						continue;
					}
					var childData = formInput.inputChecks();

					if (typeof (childData) == 'boolean' && false == childData) {
						console.info("子表单检测未通过");
						return false;
					}

					for ( var key in childData) {
						if (undefined == childDatas[tt_name]) {
							childDatas[tt_name] = new Object();
						}
						childDatas[tt_name][key] = childData[key];
					}
				}

				for ( var key in childDatas) {

					var value = childDatas[key];
					var input = $("<input type='hidden' name='childDatas._"
							+ childID + "_" + key + "'/>");
					input.val($.toJSON(value));
					$("#childDatasDiv").append(input);
				}
			}

			$('#saveform')
					.ajaxSubmit(
							function(data) {
								if ("成功保存表单数据" == data) {
									alert("成功保存表单数据");
									if ("undefined" != typeof (window.parent.listView)) {
										window.parent.listView.onRefresh();
									}
									// returnView();

									var check = onAfterSave(1, data);
									if (undefined != check
											&& "boolean" == typeof (check)
											&& true == check) {
										console.info("啥徐表单");
										instance.refreshForm();
									}
								} else if (data.indexOf("<input") == 0) {
									alert("成功保存表单数据");
									$("#refreshFormDiv").append(data);
									if ("undefined" != typeof (window.parent.listView)) {
										window.parent.listView.onRefresh();
									}
									var check = onAfterSave(1, data);
									if (undefined != check
											&& "boolean" == typeof (check)
											&& true == check) {
										instance.refreshForm();
									}
								} else {
									alert(data);
									onAfterSave(-1, data);
								}
							});
		}
		/***********************************************************************
		 * 表单提交
		 **********************************************************************/
		this.submitForm = function() {
			$('#submitform')
					.ajaxSubmit(
							function(data) {
								if ("成功执行表单提交" == data) {
									alert("成功执行表单提交");
									if ("undefined" != typeof (window.parent.listView)) {
										window.parent.listView.onRefresh();
									}
									instance.refreshForm();
								}else {
									alert(data);
								}
							});

		};
	};

	$.fn.formProcess = function(action) {
		var i, ilen, args, output, userSettings, $this = this.first(), instance = $this
				.data('formProcess');
		if (typeof action !== 'string') {
			userSettings = action || {};
			if (instance) {
				instance.updateSettings(userSettings);
			} else {
				instance = new FormProcess($this, userSettings);
				$this.data('formProcess', instance);
				instance.init();
			}
			return $this;
		} else {
			args = [];
			if (arguments.length > 1) {
				for (i = 1, ilen = arguments.length; i < ilen; i++) {
					args.push(arguments[i]);
				}
			}

			if (instance) {
				if (typeof instance[action] !== 'undefined') {
					output = instance[action].apply(instance, args);
				} else {
					throw new Error('ListView do not provide action: ' + action);
				}
			}
			return output;
		}
	}
})(jQuery);

loadResource(application + "/resource/jquery/jquery.form.js", "js");
loadResource(application + "/resource/lib/jquery.json-2.4.js", "js");

loadResource(application + "/resource/platform/skins/ace/js/designForm.js",
		"js");
loadResource(application + "/resource/platform/skins/ace/js/formOperate.js",
		"js");

loadResource(application + "/resource/platform/skins/ace/js/formInput.js", "js");

loadResource(application + "/resource/platform/skins/ace/js/checkForm.js", "js");
loadResource(application + "/resource/jquery/jquery.autocomplete.js", "js");
loadResource(application
		+ "/resource/platform/skins/ace/ckeditor/ckeditor.js", "js");
loadResource(application
		+ "/resource/platform/skins/ace/js/bootstrap.min.js", "js");

loadResource(application + "/resource/platform/skins/ace/css/tableDesign.css",
"css");
loadResource(application + "/resource/platform/skins/ace/css/bootstrap.min.css",
		"css");
loadResource(application + "/resource/platform/skins/ace/css/ace.min.css",
		"css");
loadResource(application + "/resource/platform/skins/ace/css/font-awesome.min.css",
		"css");
function loadResource(filename, filetype) {
	if (filetype == "js") { // if filename is a external JavaScript file
		var sc = "<script src='" + filename + "'></script>";
		document.write(sc);
	} else if (filetype == "css") { // if filename is an external CSS
		var sc = "<link rel='stylesheet' type='text/css' " + "href='"
				+ filename + "' />";
		document.write(sc);
	}
	if (typeof fileref != "undefined")
		document.getElementsByTagName("head")[0].appendChild(fileref)
}